/*
 * Package il.ac.biu.cs.grossmm.test.test1
 * File ServerLoader.java
 * Created on Aug 15, 2006
 *
 */
package il.ac.biu.cs.grossmm.tests.test1;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import il.ac.biu.cs.grossmm.api.flow.ActiveDataManager;
import il.ac.biu.cs.grossmm.api.flow.PersistentPointManager;
import il.ac.biu.cs.grossmm.api.server.Loader;
import il.ac.biu.cs.grossmm.api.server.ServiceManager;
import il.ac.biu.cs.grossmm.api.server.Wrapped;
import il.ac.biu.cs.grossmm.api.server.Wrapper;
import il.ac.biu.cs.grossmm.api.sql.SqlDataSource;
import il.ac.biu.cs.grossmm.impl.activeData.ActiveDataManagerImpl;
import il.ac.biu.cs.grossmm.impl.activeData.SqlPointManager;
import il.ac.biu.cs.grossmm.impl.hsqldb.DataSourceAdapter;
import il.ac.biu.cs.grossmm.impl.server.BootStrap;
import il.ac.biu.cs.grossmm.impl.server.Registry;
import il.ac.biu.cs.grossmm.api.server.Service;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class ServerLoader implements Loader, Service {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(ServerLoader.class);
    Registry registry = new Registry();
    private ServiceManager serviceManager;
    
    ServerLoader() {
        registry.register(ActiveDataManager.class, new ActiveDataManagerImpl());
        registry.register("publisher", new Publisher());
        registry.register("subscriber", new Subscriber());
        
        registry.register(PersistentPointManager.class, new SqlPointManager());
        registry.register(SqlDataSource.class, 
                new DataSourceAdapter("jdbc:hsqldb:file:presence_db", "sa", ""));
    }
    
    public Wrapped load(Wrapper wrapper, Object hint) throws Exception {
        Wrapped w = wrapper.wrap(this);
        w.setLoader(registry);
        return w;
    }

    public void start(ServiceManager serviceManager) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("start() - start");
        }
        
        logger.info("Starting server...");

        this.serviceManager = serviceManager;
        serviceManager.requireService("publisher");
        serviceManager.requireService("subscriber");

        if (logger.isDebugEnabled()) {
            logger.debug("start(ServiceManager) - end");
        }
    }

    public void stop(Exception exception) {
        logger.info("Stopping server...");
    }

    public void terminated(Object serviceHint, Exception e) {
        if( e!=null) {
            logger.error("TERMINATED " + serviceHint + " ON EXCEPTION: "+e.getMessage());
            serviceManager.terminated(e);
        }
    }
    
    public static void main(String[] args) throws Exception {
        BasicConfigurator.configure();
        
        BootStrap server = new BootStrap(new ServerLoader(), ServerLoader.class);
        
        server.start(null);
        
        //Thread.sleep(500000);
        new BufferedReader(new InputStreamReader(System.in)).readLine();
        
        server.stop(null);
    }
}
